package cn.xshi.rds.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.common.base.BaseService;
import cn.xshi.rds.dao.OMPRedisConfigDao;
import cn.xshi.rds.form.OMPRedisConfigForm;
import cn.xshi.rds.model.OMPRedisConfig;
import cn.xshi.rds.model.OMPRedisInfo;
import cn.xshi.rds.service.OMPRedisConfigService;
import cn.xshi.rds.util.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 * @Desc Redis配置
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Slf4j
@Service
public class OMPRedisConfigServiceImpl extends BaseService implements OMPRedisConfigService {

    @Resource
    OMPRedisConfigDao redisConfigDao;

    @Resource
    JedisUtil jedisUtil;

    /**
     * 初始化分页
     * @param ompRedisConfigForm
     * @return
     */
    public List<OMPRedisConfig> getRedisConfigListByCondition(OMPRedisConfigForm ompRedisConfigForm){
        return redisConfigDao.getRedisConfigListByCondition(ompRedisConfigForm);
    }

    /**
     * 查询对象
     * @param id
     * @return
     */
    public OMPRedisConfig getRedisConfigById(String id){
        return redisConfigDao.getRedisConfigById(id);
    }

    /**
     * 添加
     * @param redisConfig
     * @return
     */
    public int addRedisConfig(OMPRedisConfig redisConfig){
        int i = redisConfigDao.addRedisConfig(redisConfig);
        jedisUtil.closeRedisPool(redisConfig);
        jedisUtil.initJedisPoolMap(redisConfig);
        return i;
    }

    /**
     * 修改
     * @param redisConfig
     * @return
     */
    public int updateRedisConfig(OMPRedisConfig redisConfig){
        int i = redisConfigDao.updateRedisConfig(redisConfig);
        jedisUtil.closeRedisPool(redisConfig);
        jedisUtil.initJedisPoolMap(redisConfig);
        return i;
    }

    /**
     * delRedisConfig
     * @param condition
     * @return
     */
    public int delRedisConfig(Map<String,Object> condition){
        int i = redisConfigDao.delRedisConfig(condition);
        List<OMPRedisConfig> ompRedisConfigs = redisConfigDao.getRedisConfigList(condition);
        if(!CollectionUtil.isEmpty(ompRedisConfigs)){
            for(OMPRedisConfig ompRedisConfig:ompRedisConfigs){
                jedisUtil.closeRedisPool(ompRedisConfig);
            }
        }
        return i;
    }

    /**
     * 检测运行状态
     * @param redisConfig
     * @return
     */
    public OMPRedisConfig check(OMPRedisConfig redisConfig){
        boolean runStatus = false;
        Jedis jedis = jedisUtil.getPoolJedis(redisConfig);
        if(null != jedis){
            String info = jedis.info();
            OMPRedisInfo redisInfo = jedisUtil.convertInfo(info);
            redisConfig.setRunStatus(true);
            redisConfig.setConnectedClients(redisInfo.getConnected_clients());
            redisConfig.setRedisVersion(redisInfo.getRedis_version());
            redisConfig.setOs(redisInfo.getOs());
            redisConfig.setRedisMode(redisInfo.getRedis_mode());
            redisConfig.setTcpPort(redisInfo.getTcp_port());
            jedisUtil.closeJedis(jedis);
        }else{
            redisConfig.setConnectedClients("0");
            redisConfig.setRedisVersion("未知");
            redisConfig.setOs("未知");
            redisConfig.setRunStatus(runStatus);
            redisConfig.setRedisMode("未知");
            redisConfig.setTcpPort("未知");
        }
        return redisConfig;
    }
}
